<?php
/**################################################################################################**   
# Copyright (c)  2008  Carlos Galindo.   
# Permission is granted to copy, distribute and/or modify this document   
# under the terms of the GNU Free Documentation License, Version 1.2   
# or any later version published by the Free Software Foundation;   
# Provided 'as is' with no warranties, nor shall the autor be responsible for any mis-use of the same.     
# A copy of the license is included in the section entitled 'GNU Free Documentation License'.   
#   
#   CJAX FRAMEWORK 3.0.0RC2                $     
#   ajax made easy with cjax                    
#   -- DO NOT REMOVE THIS --                    
#   -- AUTHOR COPYRIGHT MUST REMAIN INTACT -   
#   CJAX FRAMEWORK  3.0.0RC2               $     
#   Written by: Carlos Galindo                  
#   Website: www.cjax.net                     $      
#   Email: cjxxi@msn.com    
#   Date: 2/12/2007                           $     
#   File Last Changed:  03/24/2009            $     
**####################################################################################################*/   

/**
 * pending on development
 *
 */
$core_dir = dirname(dirname(__file__));
$cjax_dir = dirname($core_dir);
require_once $core_dir.'/cjax_config.php';
$f = "$cjax_dir/../controllers/load_controller.php";
if(file_exists($f)) {
	require_once "$cjax_dir/../controllers/load_controller.php";
}
class cjax_plugin {
	
	function cjax_plugin() 
	{
		$CJAX = CJAX::getInstance();
		$method = $CJAX->get('method');
		$params = $CJAX->get('params');
		$base = CJAX_PLUGIN_DIR;    
		$plugins = CJAX_DIR.DIRECTORY_SEPARATOR."plugins".DIRECTORY_SEPARATOR; 
		$file = $base.$method;
		 
		if(file_exists($f ="$plugins$method.php")) {
			include $f;
			$js = file_get_contents($f);
			preg_match("/function\s*$method\s*\(.+\)/",$js,$out);
		
			if(!empty($out)) {
				if(!empty($params)) { 
					$params = implode(",",$params);
				}  
				echo "\n$method($params);\n";
			}
		}else if(file_exists($f = "$plugins$method/$method.php")) {
			$js = file_get_contents($f); 
			echo "$js\n";
		}else if(file_exists($f = "$plugins$method/$method.js")) {
			$js = file_get_contents($f);
			echo "$js\n";
			//$CJAX->message($js);   
		} else if(file_exists($f = $file.".php")) {
			$js = include ($f);
			$js = file_get_contents($f);  
			$echo = false;
			if(strpos($js,$method) !==false) {
				$echo = true;
			}  
			if($echo) {
				if(!empty($params)) {
					$params = implode(",",$params);
				}
				echo "\n$method($params);\n";
			}
			include "$file.php";
		} else if(file_exists($file.".js")) {
			$js = file_get_contents($file.".js");
			echo "$js\n";
		} else {
			echo "alert(\"[Non existing plugin] - $plugins$method.js not found.\");\n";
		}
		#echo("\nalert(\"{$params[1]}\");");
	}
	
	function log($log)
	{
		echo "\n\nalert(\"$log\");\n";
	}
	
}
new cjax_plugin();
